import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    redirect: 'dashboard',
    component: Home,
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('../views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard' }
      },
      {
        path: '/order/list',
        name: 'orderList',
        component: () => import('../views/order/orderListView'),
        meta: { title: '订单列表', icon: 'el-icon-s-grid' }
      },
      {
        path: '/group/add',
        name: 'groupAdd',
        component: () => import('../views/group/groupAddView'),
        meta: { title: '马上开团', icon: 'el-icon-circle-plus' }
      },
      {
        path: '/group/list',
        name: 'groupList',
        component: () => import('../views/group/groupListView'),
        meta: { title: '团购列表', icon: 'el-icon-s-grid' }
      },
      {
        path: '/funds',
        name: 'Funds',
        component: () => import('../views/funds/fundsView'),
        meta: { title: '资金中心', icon: 'el-icon-sold-out' }
      },
      {
        path: '/product/add',
        name: 'productAdd',
        component: () => import('../views/product/productAddView'),
        meta: { title: '创建商品', icon: 'el-icon-circle-plus' }
      },
      {
        path: '/product/list',
        name: 'productList',
        component: () => import('../views/product/productListView'),
        meta: { title: '我的商品', icon: 'el-icon-s-grid' }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  const isLogin = localStorage.getItem('jwt')
  if (!isLogin && to.path !== '/login') {
    next('/login')
  } else {
    next()
  }
})

export default router
